package cn.bellychang.leetcode.question387;

import java.util.HashMap;

/**
 * @author ChangLiang
 * @date 2021/3/15
 */
public class Solution2 {

    public int firstUniqChar(String s) {


        char[] chars = s.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>(chars.length);
        for (char aChar : chars) {
            if (map.containsKey(aChar)) {
                map.put(aChar, map.get(aChar) + 1);
            } else {
                map.put(aChar, 1);
            }
        }
        for (int i = 0; i < chars.length; i++) {
            if (map.get(chars[i]) == 1) {
                return i;
            }
        }
        return -1;
    }
}
